package com.ruoyi.app.controller;

import com.ruoyi.app.interceptor.SessionUtil;
import com.ruoyi.cms.domain.CrmMember;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.employee.domain.CrmEmployee;
import com.ruoyi.message.domain.MessageInfo;
import com.ruoyi.message.domain.MessageReply;
import com.ruoyi.message.domain.MessageZan;
import com.ruoyi.message.service.IMessageInfoService;
import com.ruoyi.message.service.IMessageReplyService;
import com.ruoyi.message.service.IMessageZanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论Controller
 * 
 * @author hct
 * @date 2022-04-14
 */
@RestController
@RequestMapping("/app/message")
public class AppMessageReplyController extends BaseController
{
    @Autowired
    private IMessageReplyService messageReplyService;

    @Autowired
    private IMessageInfoService messageInfoService;
    @Autowired
    private IMessageZanService messageZanService;

    /**
     * 查询评论列表
     */
     @GetMapping("/replyList")
    public TableDataInfo replyList(MessageReply messageReply)
    {
        startPage();
        List<MessageReply> list = messageReplyService.selectMessageReplyList(messageReply);
        return getDataTable(list);
    }



    /**
     * 获取评论详细信息
     */
    @GetMapping(value = "reply/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(messageReplyService.selectMessageReplyById(id));
    }

    /**
     * 新增评论
     */
    @PostMapping(value="reply/add")
    public AjaxResult add(MessageReply messageReply)
    {
        CrmMember crmEmployee=SessionUtil.getLoginUser();
        messageReply.setNickname(crmEmployee.getName());
        messageReply.setMemberid(crmEmployee.getId());
        messageReply.setHead(crmEmployee.getHead());
        messageInfoService.updateIsReplyAddById(messageReply.getMessageId());
        return toAjax(messageReplyService.insertMessageReply(messageReply));
    }



    /**
     * 删除评论
     */
    @PostMapping(value="reply/remove/{id}")
    public AjaxResult replyRemove(@PathVariable Long id)
    {
        MessageReply messageReply=messageReplyService.selectMessageReplyById(id);
        messageInfoService.updateIsReplySubById(messageReply.getMessageId());
        return toAjax(messageReplyService.deleteMessageReplyById(id));
    }
    /**
     * 新增动态
     */
    @PostMapping(value = "/add")
    public AjaxResult add(MessageInfo messageInfo)
    {
        messageInfo.setZannum(0);
        messageInfo.setSharenum(0);
        messageInfo.setReadnum(0);
        messageInfo.setCollectnum(0);
        messageInfo.setReplynum(0);
        messageInfo.setMessageStatus(0);
        CrmMember crmEmployee= SessionUtil.getLoginUser();
        if(crmEmployee!=null) {
            messageInfo.setMemberId(crmEmployee.getId());
            messageInfo.setMemberName(crmEmployee.getName());
            messageInfo.setMemberImg(crmEmployee.getHead());
        }
        return toAjax(messageInfoService.insertMessageInfo(messageInfo));
    }


    /**
     * 删除动态
     */
    @PostMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(messageInfoService.deleteMessageInfoById(id));
    }


    /**
     * 加赞和取消赞
     */
    @PostMapping("/messagezan")
    public AjaxResult messagezan(Long messageId)
    {
        MessageZan messageZan = new  MessageZan();
        CrmMember crmEmployee= SessionUtil.getLoginUser();

        messageZan.setMemberId(crmEmployee.getId());
        messageZan.setMessageId(messageId);
        List list=messageZanService.selectMessageZanList(messageZan);
        int i=0;
        if(list!=null && list.size()>0){
            MessageZan messageZan1=(MessageZan)list.get(0);
            i=messageZanService.deleteMessageZanById(messageZan1.getId());
        }else{
            i= messageZanService.insertMessageZan(messageZan);
        }
        return toAjax(i);
    }
}
